<?php
class TutorSearch extends Controller {
	function TutorSearch()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
	    $this->load->view('header');
		$this->load->view('tutor-search-form', $data);
		$this->load->view('footer');
	}
	
	function search()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $criteria = array();
	    
	    $criteria['name'] = $this->input->post('txtName') ? $this->input->post('txtName') : null;
	    $criteria['nick_name'] = $this->input->post('txtNickName') ? $this->input->post('txtNickName') : null;
	    $criteria['name_en'] = $this->input->post('txtNameEn') ? $this->input->post('txtNameEn') : null;
	    $criteria['subject'] = $this->input->post('selSubjet') ? $this->input->post('selSubjet') : null;
	    $criteria['location'] = $this->input->post('txtLocation') ? $this->input->post('txtLocation') : null;
	    $criteria['tel'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $criteria['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;
	    
		$sc = $this->input->post('chkSchool');
		$schoolwhere = "";
		if($sc) {
			$schoolwhere = " and tusc.shool_id in ( ";
			for($ii = 0; $ii < count($sc); $ii++) {
				$schoolwhere .= $sc[$ii];
				if($ii + 1 < count($sc)) {
					$schoolwhere .= ",";
				}
			}
			$schoolwhere .= ") ";
		}
		$criteria['school'] = $schoolwhere;
		
	    $this->load->model('TutorModel');
	    
		if($this->session->userdata('profile') == 'employee' && $criteria['name'] == null &&
	    	$criteria['subject'] == null && $criteria['location'] == null && $criteria['tel'] == null && 
	    	$criteria['email'] == null && $criteria['nick_name'] == null && $criteria['name_en'] == null) {
	    		$result['result'] = null; 
	    	} else {
	    		$result['result'] = $this->TutorModel->tutorSearch($criteria);
	    	}
	    $this->load->model('SchoolModel');
		$result['schoolList'] = $this->SchoolModel->getAll();
		
	    $this->load->view('header');
		$this->load->view('tutor-search-form', $result);
		$this->load->view('footer');
	}
	
	function delete(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('TutorModel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$this->TutorModel->delete($id[$i]);
	    }	    
	    $this->search();
	}
}

?>